package com.nbstar.film.spider.engine;

import com.nbstar.film.spider.domain.FilmSpiderDomain;
import com.nbstar.film.spider.excel.*;
import com.nbstar.film.spider.pojo.FilmInfo;
import com.nbstar.film.spider.spider.BaseSpider;
import com.nbstar.film.spider.spider.SpiderFactory;
import com.nbstar.film.spider.thread.ProxyThread;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class WholeSpiderEngine extends BaseSpiderEngine {

    private int colIndex = 0;
    private int seqNoIndex = 0;
    private boolean readCoyright = false;
    private boolean readSeqNo = false;
    private int copyRightIndex = 0;

    @Override
    public boolean checkDomainSucc(FilmSpiderDomain domain) {
        FilmInfo youkuFilmInfo =  domain.get(FilmSpiderDomain.SPIDER_FROM_YOUKU);
        FilmInfo iqyFilmInfo = domain.get(FilmSpiderDomain.SPIDER_FROM_IQY);
        FilmInfo qqFilmInfo = domain.get(FilmSpiderDomain.SPIDER_FROM_QQ);
        if (youkuFilmInfo != null &&
                iqyFilmInfo != null &&
                qqFilmInfo != null) {
            return youkuFilmInfo.isSuccess() && iqyFilmInfo.isSuccess() && qqFilmInfo.isSuccess();
        }

        return  false;
    }

    @Override
    public void initEngine() {
        registSpider(FilmSpiderDomain.SPIDER_FROM_DOUBAN, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_DOUBAN));
        registSpider(FilmSpiderDomain.SPIDER_FROM_IMDB, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_IMDB));
        registSpider(FilmSpiderDomain.SPIDER_FROM_TVMAO_TV, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_TVMAO_FILM));
        registSpider(FilmSpiderDomain.SPIDER_FROM_MAOYAN, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_MAOYAN));
        registSpider(FilmSpiderDomain.SPIDER_FROM_BAIKE, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_BAIKE));
        registSpider(FilmSpiderDomain.SPIDER_FROM_QQ, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_QQ));
        registSpider(FilmSpiderDomain.SPIDER_FROM_IQY, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_IQY));
        registSpider(FilmSpiderDomain.SPIDER_FROM_YOUKU, (BaseSpider) SpiderFactory.create(FilmSpiderDomain.SPIDER_FROM_YOUKU));
        //获取代理
        ProxyThread proxyThread = new ProxyThread();
        proxyThread.start();
        this.excelReader = new CommonExcelReader();
        ((CommonExcelReader)this.excelReader).setColIndex(colIndex);
        ((CommonExcelReader)this.excelReader).setSeqNoIdex(seqNoIndex);
        ((CommonExcelReader)this.excelReader).setReadSeqNo(readSeqNo);

        ((CommonExcelReader)this.excelReader).setCopyrightNum(copyRightIndex);
        ((CommonExcelReader)this.excelReader).setReadCoyright(readCoyright);

        this.excelWriter = new WholeSpiderExcelWriter(resultSheetName, resultExcelName);


        excelReader.setFilmSpiderQueue(filmSpiderQueue);
    }
}
